package com.robot.goods.service;

import java.util.ArrayList;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.robot.goods.dao.GoodsCategoryDao;
import com.robot.goods.pojo.GoodsCategory;

/**
 * category服务层
 *
 * @author wuyunbin
 */
@Service
public class GoodsCategoryService {
    @Autowired
    private GoodsCategoryDao goodsCategoryDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部并根据权重排序
     *
     * @return
     */
    public List<GoodsCategory> findAll() {
        return goodsCategoryDao.findAllByDeletedAtIsNullAndOrderBySortDesc();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<GoodsCategory> findSearch(Map whereMap, int page, int size) {
        Specification<GoodsCategory> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return goodsCategoryDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<GoodsCategory> findSearch(Map whereMap) {
        Specification<GoodsCategory> specification = createSpecification(whereMap);
        return goodsCategoryDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public GoodsCategory findById(String id) {
        return goodsCategoryDao.findById(id).orElse(null);
    }

    /**
     * 增加
     *
     * @param goodsCategory
     */
    public void add(GoodsCategory goodsCategory) {
        //雪花分布式ID生成器
        goodsCategory.setId(idWorker.nextId() + "");
        LocalDateTime now = LocalDateTime.now();
        goodsCategory.setCreatedAt(now);
        goodsCategory.setUpdatedAt(now);
        goodsCategoryDao.save(goodsCategory);
    }

    /**
     * 修改
     *
     * @param goodsCategory
     */
    public void update(GoodsCategory goodsCategory) {
        goodsCategory.setUpdatedAt(LocalDateTime.now());
        goodsCategoryDao.save(goodsCategory);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        goodsCategoryDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<GoodsCategory> createSpecification(Map searchMap) {
        return new Specification<GoodsCategory>() {
            @Override
            public Predicate toPredicate(Root<GoodsCategory> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // id
                String id = "id";
                if (searchMap.get(id) != null && !"".equals(searchMap.get(id))) {
                    predicateList.add(cb.like(root.get(id).as(String.class), "%" + (String) searchMap.get(id) + "%"));
                }
                // 上级分类
                String parentId = "parentId";
                if (searchMap.get(parentId) != null && !"".equals(searchMap.get(parentId))) {
                    predicateList.add(cb.like(root.get(parentId).as(String.class), "%" + (String) searchMap.get(parentId) + "%"));
                }
                // 分类名称
                String name = "name";
                if (searchMap.get(name) != null && !"".equals(searchMap.get(name))) {
                    predicateList.add(cb.like(root.get(name).as(String.class), "%" + (String) searchMap.get(name) + "%"));
                }
                String sort="sort";
                query.orderBy(cb.desc(root.get(sort)));
                //剔除软删除数据
                String deletedAt = "deletedAt";
                predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(LocalDateTime.class)));

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * 通过name查询GoodsCategory
     *
     * @param name
     * @return
     */
    public GoodsCategory findByName(String name) {
        return goodsCategoryDao.findByName(name);
    }
}
